<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2021/8/25
 * Time: 10:22
 */

class PlatformController extends AppController
{
    public $components = array('publicFunction','Email','Excle');
    public $layout = null;

    public function index()
    {
        $where = '';
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $limit = 10;
        $count = $this->Platform->countRotationAll($where);
        $pageCount = ceil($count / $limit);
        $list = $this->Platform->findRotationAll($page,$limit,$where);
        $this->set(array('list'=>$list,'page'=>$page,'limit'=>$pageCount));
    }

    public function alertRotation()
    {
        $params = $_POST;
        if ($params['image_url']){
            $params['create_time'] = time();
           $data =  $this->Platform->saveRotation($params);
           if ($data){
               $this->publicFunction->success($data);
           }
        }
        $this->publicFunction->fail($data);
    }

    public function delRotation()
    {
        $id = $_POST['id'];
        if ($id) {
            $res = $this->Platform->delRotation($id);
            if ($res) {
                $this->publicFunction->success($res);
            }
        }
        $this->publicFunction->fail();
    }
    /*
     * 导航栏配置
     */
    public function navigation()
    {
        $where = '';
        $limit = 10;
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $res = $this->Platform->findAllNav($page, $limit, $page);
        $count = $this->Platform->findAllCountNav($where);
        $pageCount = ceil($count / $limit);
        $this->set(array('list' => $res, 'page' => $page, 'limit' => $pageCount));
    }

    public function dellNavigation()
    {
        $params = $_POST;
        if ($params['name']){
            if ($params['id']){
                $params['update_time'] = time();
            }else{
                $params['create_time'] = time();
                $params['update_time'] = time();
            }
            $data =  $this->Platform->dellNav($params);
            if ($data){
                $this->publicFunction->success($data);
            }
        }
        $this->publicFunction->fail($data);
    }

    public function  dropNotation()
    {
        $id = $_POST['id'];
        if ($id) {
            $res = $this->Platform->dropNav($id);
            if ($res) {
                $this->publicFunction->success($res);
            }
        }
        $this->publicFunction->fail();
    }

    public function proplist()
    {
        $where = array();
        if ($_POST) {
            $startTime = $_POST['starttime'] ?: '';
            $endTime = $_POST['endtime'] ?: '';
            $prop_name = $_POST['prop_name'] ?: '';
            if ($startTime && $endTime) {
                $where['init_time BETWEEN ? and ?'] = array(strtotime($startTime), strtotime($endTime));
            }
            if ($prop_name) {
                $where['prop_name Like'] = "%{$prop_name}%";
            }
        }
        $res = array(
            'starttime' => $startTime,
            'endtime' => $endTime,
            'prop_name' => $prop_name
        );
        $page = $_GET['page'] ? $_GET['page'] : 1;
        $limit = 10;
        $count = $this->Platform->countAllProp($where);
        $pageCount = ceil($count/$limit);
        $prop = $this->Platform->findAllProp($where,$page,$limit);
        if ($_POST['export'] == "wyq"){
            $filename = "道具列表表";
            $data = [];
            foreach ($prop as $k => $v){
                $data[$k]['prop_name'] =  $v['prop_name'];
                $data[$k]['desc'] = $v['desc'];
                $data[$k]['status'] = $v['status'] ? "使用中" : "禁用";
                $data[$k]['last_time'] = $v['last_time'];
            }
            $headArr = ['道具名','道具描述','道具状态','道具最后修改时间'];
            $this->Excle->exportExcel($filename,$headArr,$data);
            exit;
        }
        $this->set(array('prop'=>$prop,'res'=>$res, 'page' => $page, 'pageCount' => $pageCount ,'url'=>'/platform/proplist'));
    }

    public function dellProp()
    {
        //修改
        $data = array(
            'prop_name' => $_POST['prop_name'],
            'desc' => $_POST['desc'],
            'status' => 1,
            'init_time' => time(),
        );
        if ($_POST['id']) {
            //修改
            $data['id'] = $_POST['id'];
        }
        if ($_FILES) {
            //图片上传地址
            $dir = PHP_OS == "WINNT" ? "D:/image/prop/" : "/var/www/data/image/prop/";
            $res = $this->publicFunction->imgUpload('image', $dir);
            $data['image'] = "/image/prop/" . $res;
        }
        $res = $this->Platform->dellProp($data);
        if ($res) {
            $this->publicFunction->success($data, '操作成功', 200);
        }
        $this->publicFunction->fail(400, '操作失败');
    }

    public function delProp()
    {
        $id = $_POST['id'];
        if (!$id){
            $this->publicFunction->fail(400,'参数错误');
        }
        $res = $this->Platform->delProp($id);
        if ($res){
            $this->publicFunction->success(200,'删除成功');
        }
        $this->publicFunction->fail(400,'删除失败');
    }

    public function next_box_dispose()
    {
        $where = [
            'activity_type' => 1
        ];
        $res = $this->Platform->getAllActivityRate($where);
        foreach ($res as $k => $v) {
            if ($v['type'] == 0) {
                $prop[] = $v;
            } else {
                $coin[] = $v;
            }
        }
        $propIdArray = array_column($prop, 'prid');
        $propIds = implode($propIdArray, ',');
        $where2 = ['id in (' . $propIds . ')'];
        $propName = $this->Platform->findPropNameByIds($where2);
//        var_dump($propName);die;
        $propName = array_column($propName, null, 'id');
        $prop = array_column($prop, null, 'prid');
        foreach ($propName as $k => $v) {
            $prop[$k]['prop_name'] = $v['prop_name'];
        }
        //获取获取魔盒的概率
        $magicGetRate = $this->Platform->getMagicAssign();
        $variable = $this->Platform->getAllProps([]);
        $props = [];
        foreach ($variable as $key => $val) {
            $props[$val['Platform']['id']] = $val['Platform']['prop_name'];
        }
        $this->set('params', ['props' => $props, 'coin' => $coin, 'prop' => $prop, 'magicGetRate' => $magicGetRate]);
    }

    public function addNextBoxRate()
    {
        $reqData = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
        if (empty($reqData['prop_id'])) {
            $this->publicFunction->response(400, '缺少参数');
        }
        $activity_type = 1;
        $type = 0;
        if ($this->Platform->existActivityRate($activity_type, $type, $reqData['prop_id'])) {
            $this->publicFunction->response(400, '该礼物已经添加过了');
        }

        $data = [
            //道具id
            'prid' => $reqData['prop_id'],
            //魔盒活动
            'activity_type' => $activity_type,
            //道具奖励
            'type' => $type,
            //概率设置为0
            'rate' => 0,
            'init_time' => time()
        ];
        $res = $this->Platform->dellActivityRate($data);
        if (!$res) {
            $this->publicFunction->response(400, '添加失败');
        }
        $this->publicFunction->response(200, '添加成功');
    }

    public function dropNextBoxRate()
    {
        $reqData = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
        if (empty($reqData['id'])) {
            $this->publicFunction->response(400, '缺少参数');
        }
        if (!$this->Platform->dropNextBoxRate($reqData['id'])){
            $this->publicFunction->response(400, '删除失败');
        }
        $this->publicFunction->response(200, '删除成功');
    }

    public function dellNextBox()
    {
        $reqData = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
        if (empty($reqData['id'])) {
            $this->publicFunction->fail(400, '缺少参数');
        }
        //查询该活动全部概率
        $where = [
            'activity_type' => 1,
            'id !=' => $reqData['id']
        ];
        $rateAll = $this->Platform->getAllRate($where);
        if ($rateAll + $reqData['rate'] > 100){
            $this->publicFunction->response(400, '概率之和不能大于100%');
        }
        $data = [
            'id' => $reqData['id'],
            'rate' => $reqData['rate'],
        ];
        if (!$this->Platform->dellActivityRate($data)){
            $this->publicFunction->response(400, '修改失败');
        }
        $this->publicFunction->response(200, '修改成功');
    }

    public function dellGetNextBox()
    {
        $reqData = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
        if (empty($reqData['action'])) {
            $this->publicFunction->response(400, '缺少参数');
        }
        if ($reqData['rate']>100 || $reqData < 0)
        {
            $this->publicFunction->response(400, '概率不能小于0，不能大于100');
        }
        $info['assign_name'] = $reqData['action'];
        $info['assign_value'] = $reqData['rate'];
        $res = $this -> Platform -> updatesafetyassign($info);
        $key = 'Rate_'.$reqData['action'];
        $redis = $this -> publicFunction -> getRedis();
        $redis -> selectDB(4);
        $redis->del($key);
        if ($res){
            $this->publicFunction->response(200, '操作成功');
        }else{
            $this->publicFunction->response(400, '操作失败');
        }
    }
}